const router = require("koa-router")();
let cache = {};
router.prefix("/comment");

function html_encode(str) {
  let s = str.replace(/&/g, "&amp;");
  s = s.replace(/\</g, "&lt;");
  s = s.replace(/\>/g, "&gt;");
  s = s.replace(/\'/g, "&#39;");
  s = s.replace(/\"/g, "&#34;");
  return s;
}

router.get("/", async (ctx, next) => {
  await ctx.render("comment", {
    title: "Hello Koa comment!",
    xss: "xss",
  });
});

router.get("/add", function (ctx, next) {
  console.log(ctx.request.query.comment, "ctx.request.query.comment");
  // cache.comment = ctx.request.query.comment;
  cache.comment = html_encode(ctx.request.query.comment);

  ctx.response.body = { success: true, code: 200 };
});
router.get("/get", function (ctx, next) {
  ctx.response.body = { success: true, code: 200, comment: cache.comment };
});

module.exports = router;
